<template>
  <van-form @submit="onSubmit">
    <van-field
        v-model="editUser.currentVal"
        :name="editUser.editKey"
        :label="editUser.editName"
        :rules="[{ required: true, message: '请输入' }]"
    />

    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>
<script setup>
import {useRoute} from "vue-router";
import {ref} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {showFailToast, showSuccessToast} from "vant";
import router from "../config/route.js";
import {getCurrentUser} from "../services/user.ts";

const route = useRoute()
const editUser = ref({
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentVal: route.query.currentVal
})

//todo 把editKey和editName提交到后太修改数据，后端有获取当前用户的方法。可以获取到id

// console.log(currentUser)
const onSubmit = async () => {
  const currentUser = await getCurrentUser()

  const res = await myAxios.put('/userInfo/updateUser', {
    'id': currentUser.id,
    [editUser.value.editKey]: editUser.value.currentVal
  })
  console.log('更新请求', res)
  if (res.code === 0 && res.data > 0) {
    router.back()
    showSuccessToast('修改成功')

  } else {
    showFailToast('修改失败')
  }
}
</script>
<style scoped>

</style>